package simulation;

import java.io.File;
import java.util.Vector;

import action.concreteActions.TerminalAction;

import locations.LocationArchive;
import person.PersonAchive;
import schedule.Schedule;
import schedule.ScheduleBuilder;

public class SimRunner {
	public static String currentDate;
	
	public static void runSimulation(String date) {
		currentDate=date;
		LocationArchive.init();
		Vector<ScheduleRunner> threads = new Vector<ScheduleRunner>();
		for (int i = 0; i < PersonAchive.getCount(); i++) {
			Schedule s = ScheduleBuilder.buildSchedule(PersonAchive
					.getPerson(i).getName(),date);
			ScheduleRunner sr = new ScheduleRunner();
			sr.init(s, i);
			threads.add(sr);
			try			
			{
				PersonAchive.getPerson(i).setInHouse(true);
				PersonAchive.getPerson(i).setCurrentLocation(((TerminalAction)s.get(0)).getLocation());
			}
			catch(Exception e)
			{
				System.out.println(PersonAchive.getPerson(i).getName());
				e.printStackTrace();
				System.out.println("Initialization error");
				System.exit(1);
			}
		}
		Schedule s=ScheduleBuilder.buildSchedule("Weather",date);
		ScheduleRunner sr = new ScheduleRunner();
		sr.init(s, PersonAchive.getCount());
		threads.add(sr);
		
		DataCollector dc = new DataCollector(currentDate);
		Thread dataThread = new Thread(dc);
		ScheduleProgram.initAccess(PersonAchive.getCount()+1);
		for (int i = 0; i < threads.size(); i++) {
			Thread scheduleThread = new Thread(threads.get(i));
			scheduleThread.start();
		}
		dataThread.start();

	}

}
